package com.ruoyi.web.controller.attend;

import com.ruoyi.attend.domain.DaySign;
import com.ruoyi.attend.domain.DayUser;
import com.ruoyi.attend.service.IDaySignService;
import com.ruoyi.attend.service.IDayUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.base.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.framework.web.base.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.web.core.wang.tool.WgrString;
import com.ruoyi.web.core.wang.utils.UUIDUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 打卡记录 信息操作处理
 *
 * @author wangxx
 * @date 2018-11-23
 */
@Controller
@RequestMapping("/attend/dayUser")
public class DayUserController extends BaseController {
    private String prefix = "attend/dayUser";

    @Autowired
    private IDayUserService dayUserService;
    @Autowired
    private IDaySignService daySignService;

    @RequiresPermissions("attend:dayUser:view")
    @GetMapping()
    public String dayUser() {
        return prefix + "/dayUser";
    }

    /**
     * 查询打卡记录列表
     */
    @RequiresPermissions("attend:dayUser:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(DayUser dayUser) {
        startPage();
        List<DayUser> list = dayUserService.selectDayUserList(dayUser);
        return getDataTable(list);
    }

    /**
     * 导出打卡记录列表
     */
    @RequiresPermissions("attend:dayUser:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(DayUser dayUser) {
        List<DayUser> list = dayUserService.selectDayUserList(dayUser);
        ExcelUtil<DayUser> util = new ExcelUtil<DayUser>(DayUser.class);
        return util.exportExcel(list, "dayUser");
    }

    /**
     * 新增打卡记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存打卡记录
     */
    @RequiresPermissions("attend:dayUser:add")
    @Log(title = "打卡记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(DayUser dayUser) {
        dayUser.setLoginIp(ShiroUtils.getIp());
        dayUser.setCreateBy(ShiroUtils.getLoginName());
        //dayUser.setSignId(WgrString.getRandomNumber(5));//随机号
        dayUser.setCardUserId(Integer.valueOf(WgrString.getRandomNumber(5)));//随机号
        //保存到签到表中
        DaySign daySign=new DaySign();
        daySign.setUserId(dayUser.getCardUserId().toString());
        daySign.setSignId(UUIDUtil.getUUID()); //要设置成uuid
        daySignService.insertDaySign(daySign);
        return toAjax(dayUserService.insertDayUser(dayUser));
    }

    /**
     * 修改打卡记录
     */
    @GetMapping("/edit/{cardUserId}")
    public String edit(@PathVariable("cardUserId") Integer cardUserId, ModelMap mmap) {
        DayUser dayUser = dayUserService.selectDayUserById(cardUserId);
        mmap.put("dayUser", dayUser);
        return prefix + "/edit";
    }

    /**
     * 记录打卡次数
     **/
   /* @RequiresPermissions("attend:dayUser:attend:dayUser:attendPlus")
    @PostMapping("/attendPlus")
    @Log(title = "打卡记录", businessType = BusinessType.OTHER)
    @ResponseBody
    public AjaxResult attendUser(String ids) {
        String[] userIds = Convert.toStrArray(ids);
        DayUser dayUser =null;
        for (String userId : userIds) {
            dayUser=dayUserService.selectDayUserById(Integer.valueOf(userId));
            if (dayUser.getAttendCount() != null) {
                Integer num = Integer.valueOf(dayUser.getAttendCount())+1;
                dayUser.setAttendCount(num.toString());
            }else {
                dayUser.setAttendCount("1");
            }
            dayUserService.updateDayUser(dayUser);
        }
        return toAjax(dayUser.getCardUserId());
    }*/

    /**
     * 修改保存打卡记录
     */
    @RequiresPermissions("attend:dayUser:edit")
    @Log(title = "打卡记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(DayUser dayUser) {
        return toAjax(dayUserService.updateDayUser(dayUser));
    }

    /**
     * 删除打卡记录
     */
    @RequiresPermissions("attend:dayUser:remove")
    @Log(title = "打卡记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(dayUserService.deleteDayUserByIds(ids));
    }

    @RequestMapping("/addPhoto")
    @ResponseBody
    public DayUser uploadImage(DayUser dayUser, @RequestParam(value = "file") MultipartFile file) {
        return dayUserService.uploadImage(dayUser, file);
    }
}
